package com.bing.springboot.controller;

import com.bing.springboot.bean.Monster;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

/**
 * @author 徐
 * @version 1.0
 * 接收参数演示
 */
@RestController
public class ParameterController {

    /**
     * 1. /monster/{id}/{name} 构成完整请求路径
     * 2. {id} {name} 是占位变量
     * 3. @PathVariable("name") : 这里 name 和 {name} 命名保持一致
     * 4. @PathVariable Map<String,String> map 把所有传递的值传入map
     *    map-{id=100, name=king}
     * @param id
     * @param name
     * @param map
     * @return
     */
    @GetMapping("/monster/{id}/{name}")
    public String pathVariable(@PathVariable("id") Integer id,
                               @PathVariable("name") String name,
                               @PathVariable Map<String,String> map){
        System.out.println("id-" + id);
        System.out.println("name-" + name);
        System.out.println("map-" + map);
        return "success";
    }

    /**
     * @RequestHeader("Host") 作用是获取http请求头的 host 信息
     * @RequestHeader Map<String,String> header 获取http请求的所有信息
     * @param host
     * @param header
     * @return
     */
    @GetMapping("/requestHeader")
    public String requestHeader(@RequestHeader("Host") String host,
                                @RequestHeader Map<String,String> header,
                                @RequestHeader("accept") String accept){

        System.out.println("host==" + host);
        System.out.println("header==" + header);
        System.out.println("accept==" + accept);
        return "success";
    }

    /**
     * 1. @RequestParam 获取携带参数
     * 2. required = false 可以不提交此参数
     * 3. @RequestParam Map<String,String>
     *     paras 获取所有参数，但相同参数名，对应的值只有一个
     * paras = {name=ted, fruit=apple}
     * @param username
     * @param fruits
     * @param paras
     * @return
     */
    @GetMapping("/hi")
    public String hi(@RequestParam(value = "name",required = false) String username,
                     @RequestParam(value = "fruit") List<String> fruits,
                     @RequestParam Map<String,String> paras){
        System.out.println("usetname = " + username);
        System.out.println("fruits = " + fruits);
        System.out.println("paras = " + paras);
        return "success";
    }

    /**
     * 因为我的浏览器目前没有cookie，我们可以自己设置cookie
     * 如果要测试，可以先写一个方法，在浏览器创建对应的cookie
     * 说明1．value ＝＂cookie＿key＂ 表示接收名字为 cookie＿key的 cookie
     * 2．如果浏览器携带来对应的cookie，那么后面的参数是String，则接收到的是对应对value
     * 3．后面的参数是Cookie，则接收到的是封装好的对应的cookie
     * @param cookie
     * @return
     */
    @GetMapping("/cookie")
    public String cookie(@CookieValue(value = "cookie_key",required = false) String cookie_key,
                         @CookieValue(value = "username",required = false) Cookie cookie,
                         HttpServletRequest request){
        System.out.println("cookie_key = " + cookie_key);
        System.out.println("cookie = " + cookie.getName() + "-" + cookie.getValue());
        //原生 HttpServletRequest 获取cookie
        Cookie[] cookies = request.getCookies();
        for (Cookie cookie1 : cookies) {
            System.out.println(cookie1.getValue());
        }
        return "success";
    }


    /**
     * 获取请求体信息
     * @param content
     * @return
     */
    @PostMapping("/save")
    public String postMethod(@RequestBody String content){
        System.out.println("content= " + content);
        return "success";
    }

    //处理添加monster的方法
    @PostMapping("/savemonster")
    public String saveMonster(Monster monster){
        //monster-Monster(id=100, name=牛魔王, age=500, isMarried=true,
        // birth=Sat Nov 11 00:00:00 CST 2000, car=Car(name=白龙马, price=50000.0))
        System.out.println("monster-" + monster);
        return "success";
    }

}
